script "Superdrinks for Fun and Profit!";
notify fluxxdog;
since <16.5>;
import <zlib.ash>;

#For more information, see http://kolmafia.us/showthread.php?4703
setvar("superdrink_preferred","none");
/* "superdrink_preferred" notes the drink you want to make the most of. Simply set it something
 * like "Mae West" and it'll make as many Maes West as it can before moving on to other drinks.
 * If you're wanting to make drinks only for profit, you probably want to set this to "none".
 * Others options include:
 * garnish = prefer drinks that use this kind of garnish, such as coconut shell
 * most = make drinks with the garnish you have the most of
 * stat = make drinks of only a certain stat, such as moxie
 * single = make drinks that only have a single stat
 * day = make drinks that focus on today's stat bonus, else make single stat drinks
 * refine = make drinks with the garnish you have the most of, but only have a single stat
 * sake = make only infused sake drinks. This overrides "superdrink_use_sake", see below.
 * salty = make Salacious cocktails. This overrides "superdrink_use_salty", see below. Also, when you can't make
 * 		 any more salty drinks, the script will then make single stat only drinks.*/
setvar("superdrink_only_preferred",false);
/* only_preferred, when set to true, will stop making drinks if you can't make any preferred drinks
 * If you limit the kinds of drinks it can make and don't have mall buying turned on, it may not
 * be able to make any drinks.*/
setvar("superdrink_use_sake",false); /* This considers infused sake drinks and salacious cocktails
 * If true, it will look at these drinks as options as well. There could be some major money
 * to consider here if you're willing to get the ingredients or buy them. This will skirt around
 * SHC drinks, such as using garnishes.
 * If you set "sake" as a drink preference, this setting is always considered true.*/
setvar("superdrink_use_salty",false); /* This considers making Salacious Cocktails. As sake drinks can provide
 * more powerful benefits over salty drinks, you may not want to make them. This will enable the script
 * to process them.
 * If you set "salty" as a drink preference, this setting is always considered true.*/
setvar("superdrink_use_ooze-o",false); /* This considers making Ooze-o for sewer runs (or profit), which can be
 * handy if you're focusing on the Hobopolis dungeon.
 * If you set "ooze-o" as a drink preference, this setting is always considered true.*/
setvar("superdrink_tempmall",false);
/* "superdrink_tempmall" will allow this script to
 * temporarily circumvent your mall settings. If you don't like automatically buying from the mall
 * but want to explore the most avenues of profit with this, then set this to true. This is
 * not for people with very little meat, however. There is a check included to keep from overspending
 * and it takes still and adventure values into account.*/
setvar("superdrink_accuracy",3.0); /* This is used to gauge the accuracy of the prices that determine
 * profit margins. Higher numbers mean less hits on the servers, but lowers accuracy. Lower numbers
 * will give more current data, but put more of a load on the servers. a setting of 3.0 should be good
 * enough. Please don't go any lower than 1.0 though. */
setvar("superdrink_profit","no");
/* "superdrink_profit determines whether you make drinks by availability
 * or by profitability:
	//yes,true will find the highest profit drinks and make them
	//no,false (or anything else) will make various drinks to keep
 * Profit value can be adjusted by setting the properties "valueOfStill" and "valueOfAdventure"
 * It should calculate what drinks you can make the most profit on and make them if set properly.
	//clan,stash will put all the drinks you made into (you guessed it) the clan stash.
 * This is good for bots that don't really need to drink the drinks or for people who are very
 * generous with their clan. This setting is checked against can_interact(), so you won't put drinks
 * in the stash that you need for your run, but if you're out of Ronic/Hardcore, you can get your drinks
 * easily if need be.
	//auto,mall will make the highest profit drinks, and put them in your store for 95% of the lowest price
 * This script can also be set to automatically mall sell the drinks you made. It does this by shoving
 * them in your mall store at 95% of the historical price to help make a fast sale. It will NOT 
 * sell drinks if you are in Ronin or Hardcore. This safety keeps your drinks from being sold when you
 * could really use them.*/
//Finish setting preferences
//Prime the preferences for use
string superdrink_preference=to_lower_case(vars["superdrink_preferred"]);
boolean superdrink_tempmall=false;
boolean superdrink_canImallbuy=false;
boolean superdrink_sell_all_day=false;
boolean superdrink_judge_value=false;
boolean superdrink_clan_stash=false;
boolean superdrink_use_sake=to_boolean(vars["superdrink_use_sake"]);
boolean superdrink_use_salty=(to_boolean(vars["superdrink_use_salty"]) && have_skill($skill[Salacious Cocktailcrafting]));
boolean superdrink_use_oozeo=to_boolean(vars["superdrink_use_ooze-o"]);
boolean superdrink_clan_access=to_boolean(get_property("autoSatisfyWithClan")) && get_clan_name()!="";

void superdrinks_primer(){
	if(can_interact()){
		if(!to_boolean(get_property("autoSatisfyWithMall")) && to_boolean(vars["superdrink_tempmall"])){
			vprint("Setting \"autoSatisyWithMall\" temporarily to true","blue",4);
			set_property("autoSatisfyWithMall",true);
			superdrink_tempmall=vars["superdrink_tempmall"].to_boolean();
		} //If allowing this script to use mall when not normally...
		if(!to_boolean(get_property("autoSatisfyWithMall"))){
			vprint("This script can be used to buy ingredients from the mall. Type \"set autoSatifyWithMall=true\" to do so.","purple",6);
			vprint("You can also type \"zlib superdrink_tempmall = true\" to temporarily allow mall buying with this script.","purple",6);
		} else superdrink_canImallbuy=true;
	}
	vprint("superdrink_canImallbuy = "+superdrink_canImallbuy,"#FFA500",10);
	boolean drink_match(){
		if(superdrink_preference!=to_lower_case(to_item(superdrink_preference))){
			vprint("Your superdrink_preferred setting is not set correctly! Use \"zlib superdrink_preferred = \" to properly set it!","red",1);
			return false;
		}
		return true;
	}
	switch{
	case superdrink_preference=="none":
		superdrink_preference="any:none"; break;
	case contains_text(superdrink_preference,"coconut"):
	case contains_text(superdrink_preference,"shell"):
		superdrink_preference="garnish:coconut shell"; break;
	case contains_text(superdrink_preference,"little"):
	case contains_text(superdrink_preference,"paper"):
	case contains_text(superdrink_preference,"umbrella"):
		superdrink_preference="garnish:little paper umbrella"; break;
	case contains_text(superdrink_preference,"magical"):
	case contains_text(superdrink_preference,"ice"):
	case contains_text(superdrink_preference,"cubes"):
		superdrink_preference="garnish:magical ice cubes"; break;
	case contains_text(superdrink_preference,"most"):
		superdrink_preference="garnish:most"; break;
	case contains_text(superdrink_preference,"day"):
		if(stat_bonus_today()!=$stat[none]) superdrink_preference="stat:"+stat_bonus_today();
		else superdrink_preference="stat:single";
		break;
	case contains_text(superdrink_preference,"mus"):
		superdrink_preference="stat:muscle"; break;
	case contains_text(superdrink_preference,"mys"):
		superdrink_preference="stat:mysticality"; break;
	case contains_text(superdrink_preference,"mox"):
		superdrink_preference="stat:moxie"; break;
	case contains_text(superdrink_preference,"single"):
		superdrink_preference="stat:single"; break;
	case contains_text(superdrink_preference,"refine"):
		superdrink_preference="refine:most"; break;
	case contains_text(superdrink_preference,"sake"):
		superdrink_use_sake=true;
		if(!(contains_text(superdrink_preference,"-infused"))) superdrink_preference="sake:any";
		else if(drink_match()) superdrink_preference="drink:"+to_lower_case(vars["superdrink_preferred"]);
		else superdrink_preference="fullstop";
		break;
	case contains_text(superdrink_preference,"salacious"):
		if(have_skill($skill[Salacious Cocktailcrafting])){
			superdrink_use_salty=true;
			if(drink_match()) superdrink_preference="drink:"+to_lower_case(vars["superdrink_preferred"]);
			else superdrink_preference="fullstop";
		}
		else superdrink_preference="stat:single";
		break; //prevent failure because they ascended and forgot to reset settings
	case contains_text(superdrink_preference,"salty"):
		if(have_skill($skill[Salacious Cocktailcrafting])){
			superdrink_use_salty=true;
			superdrink_preference="salty:any";
		}
		else superdrink_preference="stat:single";
		break;
	case contains_text(superdrink_preference,"ooze-o"):
	case contains_text(superdrink_preference,"oozeo"):
		superdrink_preference="drink:bottle of Ooze-O";
		superdrink_use_oozeo=true; break;	
	default:
		if(drink_match()) superdrink_preference="drink:"+to_lower_case(vars["superdrink_preferred"]);
		else superdrink_preference="fullstop"; return;
	}
	vprint("superdrink_preference = "+superdrink_preference,"#FFA500",10);
	if(!superdrink_canImallbuy && superdrink_preference!="any:none"){ //No mall for specific drinks? Alert!
		if(can_interact()) vprint("Without mall buying, it may be difficult to make some drinks","red",2); //Unless in ronin
	}
	switch(to_lower_case(vars["superdrink_profit"])){
	case "auto":
	case "mall":
		superdrink_judge_value=true;
		if(have_shop() && can_interact()) superdrink_sell_all_day=true;
		break;
	case "yes":
	case "true": superdrink_judge_value=true; break;
	case "clan":
	case "stash": if(get_clan_name()!="" && can_interact()) superdrink_clan_stash=true; break;
	default: break;
	}
	if(superdrink_clan_access) refresh_stash();
}
vprint("superdrink_judge_value = "+superdrink_judge_value,"#FFA500",10);
vprint("superdrink_sell_all_day = "+superdrink_sell_all_day,"#FFA500",10);
vprint("superdrink_clan_stash = "+superdrink_clan_stash,"#FFA500",10);

boolean bellyup(){
	if(to_boolean(get_property("requireBoxServants")) && !have_bartender()){
		vprint("Your bartender is gone and I ain't your lackey!","red",1);
		return false;
	}
	return true;
}

void last_call(){
	if(superdrink_tempmall && get_property("autoSatisyWithMall")!="false"){
		vprint("Setting \"autoSatisyWithMall\" back to false","blue",4);
		set_property("autoSatisfyWithMall",false);
	}
}
//Finish priming
//Build the bartender's guide
//First, let's define our data group and record maps
record { item basebooze; item baseingredient; } [item] superbooze;
record { item basefruit; } [item] superfruit;
record { item garnish; item SAdrink; item superbooze; item superfruit; stat preftype;} [item] Superdrink;
record { item seafruit; item superbooze; stat preftype;} [item] Supersake;
record { item seaode; item seafruit; item slugbooze; stat preftype;} [item] Supersalty;
//Next, let's flesh out the ingredient lists
void bartenders_guide(){
	stat pref_set(item drinky){
		if(drinky.muscle!="0" && drinky.mysticality=="0" && drinky.moxie=="0") return $stat[muscle];
		else if(drinky.muscle=="0" && drinky.mysticality!="0" && drinky.moxie=="0") return $stat[mysticality];
		else if(drinky.muscle=="0" && drinky.mysticality=="0" && drinky.moxie!="0") return$stat[moxie];
		return $stat[none];
	}
	foreach bigbooze in $items[bottle of Calcutta Emerald,bottle of Lieutenant Freeman,bottle of Jorge Sinsonte,
	bottle of Definit,bottle of Domesticated Turkey,boxed champagne,bottle of Pete's Sake]{
		foreach shakedown,z in get_ingredients(bigbooze){
			superbooze[bigbooze].basebooze=shakedown;
			foreach teardown,w in get_ingredients(superbooze[bigbooze].basebooze){
				if(item_type(teardown)=="food") superbooze[bigbooze].baseingredient=teardown;
			}
		}
	}
	foreach bigfruit in $items[tangerine,kiwi,cocktail onion,kumquat,tonic water,raspberry]{
		foreach ripdown,q in get_ingredients(bigfruit){
			superfruit[bigfruit].basefruit=ripdown;
		}
	}
	foreach drinkname in $items[Mae West,Divine,Gordon Bennett,Mon Tiki,Neuromancer,gimlet,mandarina colada,
						   prussian cathouse,tangarita,teqiwila slammer,vodka stratocaster,yellow brick road]{
		foreach ingred,x in get_ingredients(drinkname){
			if(item_type(ingred)=="booze"){
				Superdrink[drinkname].SAdrink=ingred;
				foreach breakdown,y in get_ingredients(ingred){
					if(item_type(breakdown)=="booze") Superdrink[drinkname].superbooze=breakdown;
					else Superdrink[drinkname].superfruit=breakdown;
				}
			}
			else Superdrink[drinkname].garnish=ingred;
		}
		Superdrink[drinkname].preftype=pref_set(drinkname);
	}
	if(superdrink_use_sake) { //If we're not making sake, don't bother
		foreach drinkname in $items[berry-infused sake,citrus-infused sake,melon-infused sake]{
			foreach breakdown,x in get_ingredients(drinkname){
				if(item_type(breakdown)=="food") Supersake[drinkname].seafruit=breakdown;
			}
			Supersake[drinkname].superbooze=$item[bottle of Pete's Sake];
			if(drinkname.muscle!="0" && drinkname.mysticality=="0" && drinkname.moxie=="0") Superdrink[drinkname].preftype=$stat[muscle];
			else if(drinkname.muscle=="0" && drinkname.mysticality!="0" && drinkname.moxie=="0") Superdrink[drinkname].preftype=$stat[mysticality];
			else if(drinkname.muscle=="0" && drinkname.mysticality=="0" && drinkname.moxie!="0") Superdrink[drinkname].preftype=$stat[moxie];
			else Superdrink[drinkname].preftype=$stat[none];
			Supersake[drinkname].preftype=pref_set(drinkname);
		}
	}
	if(superdrink_use_salty){ //If we're not making salty drinks, don't bother
		foreach drinkname in $items[salacious screwdiver,dew yoana salacious lei,salacious lychee chuhai]{
			foreach breakdown,x in get_ingredients(drinkname){
				if(item_type(breakdown)=="booze"){
					foreach teardown,y in get_ingredients(breakdown){
						if(item_type(breakdown)=="booze") Supersalty[drinkname].slugbooze=teardown;
						if(item_type(breakdown)=="food") Supersalty[drinkname].seafruit=teardown;
					}
				}
			}
			Supersalty[drinkname].seaode=$item[seaode];
		Supersalty[drinkname].preftype=pref_set(drinkname);
		}
	}
}
//Bartender's guide done
//Check for some needed items.
boolean prereq_supermixer(){
	superdrinks_primer();
	if(superdrink_preference=="fullstop") return false;
	if(!bellyup()) return false;
	if(!to_boolean(get_property("autoSatisfyWithNPCs"))){
		vprint("superdrinks.ash should be allowed to buy from NPCs to run properly. Type \"set autoSatifyWithNPCs=true\" to do so.","red",2);
	}
	if(!have_skill($skill[Advanced Cocktailcrafting]) && !have_skill($skill[Mixologist])){ //Thanks to camperdave for the Mixologist
		vprint("Learn Advanced Cocktailcrafting or Mixologist before you try the super stuff!","red",1);
		return false; //Requires basic skills first and foremost
	}
	if(stills_available()==0) switch{ //Why?
		case !have_skill($skill[Superhuman Cocktailcrafting]) && my_class()!=$class[Avatar of Sneaky Pete]:
			vprint("Learn how to use the still first!","red",1); return false; //Need Super skill (or be cool)
		case my_class()==$class[Disco Bandit]:
		case my_class()==$class[Accordion Thief]:
			if(!guild_store_available()){
				vprint("You need to unlock your guild first!","red",1);
				return false; //If no store, then definitely no stills
			}
		case my_class()==$class[Avatar of Sneaky Pete]:
			vprint("You can use the still again after rollover.","red",1); return false; //Just none left till tomorrow
		default:
			vprint("You can't use the still unless you're a Disco Bandit or an Accordion Thief!","red",1);
			vprint("Avatar of Sneaky Pete can use this also.","red",1);
			return false;
	}
	bartenders_guide();
	return true;
}
//Calculate the cost of drinks
int[item]superdrink_buildprice; //How much does it cost to make an item?
int turnprice(){ //Calculate turn costs if we have no bartender
	vprint("turnpice() = "+(to_int(get_property("valueOfAdventure"))*to_int(!have_bartender())),"#FFA500",10);
	return to_int(get_property("valueOfAdventure"))*to_int(!have_bartender());
}
int cost_of_business(item prospect){ //How much can you buy it for?
	if(historical_age(prospect)>to_float(vars["superdrink_accuracy"])) mall_price(prospect);
	vprint("historical_price("+prospect+") = "+historical_price(prospect),"#FFA500",10);
	return (historical_price(prospect));
}
int need_to_use(item thingie){ //If I don't have it, how much will it cost to get?
	if(superdrink_clan_access && stash_amount(thingie)>=1) return 0;
	if(!to_boolean(have_item(thingie)+available_amount(thingie))) return cost_of_business(thingie);
	return 0;
}
int superboozecost(item shot){ //Calculates cost of creating a superbooze (bottle of Domesticated Turkey)
	superdrink_buildprice[shot]=min(need_to_use(shot),to_int(get_property("valueOfStill"))+need_to_use(superbooze[shot].basebooze));
	superdrink_buildprice[shot]=min(superdrink_buildprice[shot],to_int(get_property("valueOfStill"))+(ceil(need_to_use(superbooze[shot].baseingredient)/3)+ceil(need_to_use($item[fermenting powder])/3)));
	return superdrink_buildprice[shot];
}
int superfruitcost(item drinkname){ //Price the various Superfruits (raspberry)
	return min(need_to_use(Superdrink[drinkname].superfruit),to_int(get_property("valueOfStill"))+need_to_use(superfruit[Superdrink[drinkname].superfruit].basefruit));
}
int SuperProfit(item shot){ //If you sold a superdrink, how much could you make off of it?
	superdrink_buildprice[shot] =need_to_use(Superdrink[shot].garnish)+turnprice();
	superdrink_buildprice[shot]+=min(need_to_use(Superdrink[shot].SAdrink),superboozecost(Superdrink[shot].superbooze)+superfruitcost(shot)+turnprice());
	vprint("95% of mall price for "+shot+" = "+floor(cost_of_business(shot)*95/100),"#FFA500",10);
	vprint("buildprice of "+shot+" = "+superdrink_buildprice[shot],"#FFA500",10);
	vprint("profit potential for "+shot+" is "+((floor(cost_of_business(shot)*95/100))-superdrink_buildprice[shot])+" meat","#FFA500",10);
	return (floor(cost_of_business(shot)*95/100))-superdrink_buildprice[shot];
}
int SakeProfit(item shot){ //If you sold an infused sake, how much could you make off of it?
	superdrink_buildprice[shot]=need_to_use(Supersake[shot].seafruit)+superboozecost(Supersake[shot].superbooze)+turnprice();
	vprint("95% of mall price for "+shot+" = "+floor(cost_of_business(shot)*95/100),"#FFA500",10);
	vprint("buildprice of "+shot+" = "+superdrink_buildprice[shot],"#FFA500",10);
	vprint("profit potential for "+shot+" is "+((floor(cost_of_business(shot)*95/100))-superdrink_buildprice[shot])+" meat","#FFA500",10);
	return (floor(cost_of_business(shot)*95/100))-superdrink_buildprice[shot];
}
int SaltyProfit(item shot){ //If you sold a salacious drink, how much could you make off of it?
	superdrink_buildprice[shot] =need_to_use(Supersalty[shot].seafruit);
	superdrink_buildprice[shot]+=need_to_use(Supersalty[shot].slugbooze);
	superdrink_buildprice[shot]+=need_to_use(Supersalty[shot].seaode);
	superdrink_buildprice[shot]+=(turnprice()*2);
	vprint("95% of mall price for "+shot+" = "+floor(cost_of_business(shot)*95/100),"#FFA500",10);
	vprint("buildprice of "+shot+" = "+superdrink_buildprice[shot],"#FFA500",10);
	vprint("profit potential for "+shot+" is "+((floor(cost_of_business(shot)*95/100))-superdrink_buildprice[shot])+" meat","#FFA500",10);
	return (floor(cost_of_business(shot)*95/100))-superdrink_buildprice[shot];
}
int oozeProfit(){
	superdrink_buildprice[$item[bottle of Ooze-O]]=need_to_use($item[bottle of sewage schnapps])+to_int(get_property("valueOfStill"));
	vprint("95% of mall price for "+$item[bottle of Ooze-O]+" = "+floor(cost_of_business($item[bottle of Ooze-O])*95/100),"#FFA500",10);
	vprint("buildprice of "+$item[bottle of Ooze-O]+" = "+superdrink_buildprice[$item[bottle of Ooze-O]],"#FFA500",10);
	vprint("profit potential for "+$item[bottle of Ooze-O]+" is "+((floor(cost_of_business($item[bottle of Ooze-O])*95/100))-superdrink_buildprice[$item[bottle of Ooze-O]])+" meat","#FFA500",10);
	return (floor(cost_of_business($item[bottle of Ooze-O])*95/100))-superdrink_buildprice[$item[bottle of Ooze-O]];
}
int time_to_profit(item shot){
	if(Superdrink contains shot) return SuperProfit(shot);
	if(Supersake contains shot) return SakeProfit(shot);
	if(Supersalty contains shot) return SaltyProfit(shot);
	if(shot==$item[bottle of Ooze-o]) return oozeProfit();
	return 0;
}
//Done calculating
//Strategically buy and make ingredients
void Superbuy(item shot){ //This is one big IF factory!
	if(!to_boolean(have_item(Superdrink[shot].garnish)+available_amount(Superdrink[shot].garnish))) retrieve_item(1,Superdrink[shot].garnish);
	if(need_to_use(Superdrink[shot].SAdrink)<=(superboozecost(Superdrink[shot].superbooze)+superfruitcost(shot))+turnprice()){
		retrieve_item(1,Superdrink[shot].SAdrink);
	}
	else {
		if(need_to_use(Superdrink[shot].superbooze)<=to_int(get_property("valueOfStill"))+need_to_use(superbooze[Superdrink[shot].superbooze].basebooze)){
			retrieve_item(1,Superdrink[shot].superbooze);
		}
		else {
			if(need_to_use(superbooze[Superdrink[shot].superbooze].basebooze)<=(ceil(need_to_use(superbooze[Superdrink[shot].superbooze].baseingredient)/3)+ceil(need_to_use($item[fermenting powder])/3))){
				retrieve_item(1,superbooze[Superdrink[shot].superbooze].basebooze);
			} else {
				retrieve_item(1,superbooze[Superdrink[shot].superbooze].baseingredient);
				retrieve_item(1,$item[fermenting powder]);
				create(1,superbooze[Superdrink[shot].superbooze].basebooze);
			}
			create(1,Superdrink[shot].superbooze);
		}
		if(need_to_use(Superdrink[shot].superfruit)<=to_int(get_property("valueOfStill"))+need_to_use(superfruit[Superdrink[shot].superfruit].basefruit)){
			retrieve_item(1,Superdrink[shot].superfruit);
		} else {
			retrieve_item(1,superfruit[Superdrink[shot].superfruit].basefruit);
			create(1,Superdrink[shot].superfruit);
		}
	create(1,Superdrink[shot].SAdrink);
	}
}
void Sakebuy(item shot){ //Not as iffy
	if(need_to_use(Supersake[shot].superbooze)<=to_int(get_property("valueOfStill"))+need_to_use(superbooze[Supersake[shot].superbooze].basebooze)){
		retrieve_item(1,Superdrink[shot].superbooze);
	} else {
		if(need_to_use(superbooze[Supersake[shot].superbooze].basebooze)<=(ceil(need_to_use(superbooze[Supersake[shot].superbooze].baseingredient)/3)+ceil(need_to_use($item[fermenting powder])/3))){
			retrieve_item(1,superbooze[Supersake[shot].superbooze].basebooze);
		}
		else {
			retrieve_item(1,superbooze[Supersake[shot].superbooze].baseingredient);
			retrieve_item(1,$item[fermenting powder]);
			create(1,superbooze[Supersake[shot].superbooze].basebooze);
		}
		create(1,Superdrink[shot].superbooze);
	}
	retrieve_item(1,Supersake[shot].seafruit);
}
void Saltybuy(item shot){ //No ifs... I am not your puppet!
	retrieve_item(1,Supersalty[shot].seaode);
	retrieve_item(1,Supersalty[shot].seafruit);
	retrieve_item(1,Supersalty[shot].slugbooze);
}
void time_to_buy(item shot){ //Which kind of drink do we need to buy?
	if(Superdrink contains shot) Superbuy(shot);
	if(Supersake contains shot) Sakebuy(shot);
	if(Supersalty contains shot) Saltybuy(shot);
	if(shot==$item[bottle of Ooze-o]) retrieve_item(1,$item[bottle of sewage schnapps]);
}
//Finished strategy
//Meat and potatoes!
void supermixer(){
	try{
		int[item]drinks_made;
		while(stills_available() > 0 && bellyup()){
			clear(superdrink_buildprice); item winelist=$item[none]; int winelist_profit=0; //reset
			vprint("superdrink_preference = "+superdrink_preference,"#FFA500",10);
			boolean double_check(item chuggable){ //Make sure the drink meets our criteria
				item garnish_check=$item[none];
				string stat_check="";
				switch (excise(superdrink_preference,"",":")){
				case "any": return true; //Spread drink making
				case "garnish": //Focus os garnishes
					if(!(Superdrink contains chuggable)) return true; //Sake and Salty drinks don't get garnishes
					if(excise(superdrink_preference,":","")=="most"){
						foreach topping in $items[coconut shell,little paper umbrella,magical ice cubes]{
							if(available_amount(topping)>=available_amount(garnish_check)){
								garnish_check=topping;
							}
						}
					}
					else garnish_check=to_item(excise(superdrink_preference,":",""));
					if(Superdrink[chuggable].garnish==garnish_check) return true;
					break;
				case "stat": //focus on stats
					if(Superdrink contains chuggable) stat_check=Superdrink[chuggable].preftype;
					else if(Supersake contains chuggable) stat_check=Supersake[chuggable].preftype;
					else stat_check=Supersalty[chuggable].preftype;
					if(excise(superdrink_preference,":","")=="single" && stat_check!="") return true;
					else if(excise(superdrink_preference,":","")==stat_check) return true;
					break;
				case "refine": //focus on most garnishes and single stats
					if(Superdrink contains chuggable) stat_check=Superdrink[chuggable].preftype;
					else return true; //Sake and Salty don't need garnishes and are all single stat
					foreach topping in $items[coconut shell,little paper umbrella,magical ice cubes]{
						if(available_amount(topping)>=available_amount(garnish_check)){
							garnish_check=topping;
						}
					}
					if(Superdrink[chuggable].garnish==garnish_check && stat_check!="") return true;
					break;
				case "sake": if(Supersake contains chuggable) return true; break; //Focus on sake
				case "salty": if(Supersalty contains chuggable) return true; break; //focus on salty drinks
				case "drink": if(chuggable==to_item(excise(superdrink_preference,":",""))) return true; break;
				}
				return false;
			}

			void evaluate(item concoction){ //Determine which drink we'll make
				if(!double_check(concoction)) return;
				if(to_boolean(creatable_amount(concoction))||superdrink_canImallbuy){ //If I can buy stuff, doesn't matter if I can make it alone
					if(winelist==$item[none]) winelist=concoction; //If we can make at least 1 drink, make it!
					if(superdrink_judge_value){ //If we're looking for profit..
						int temp_profit=time_to_profit(concoction);
						if(winelist_profit<temp_profit){
							winelist=concoction;
							winelist_profit=temp_profit;
						}
					}
					else if(available_amount(concoction) <= available_amount(winelist)) //check to see which we have the least of
						winelist=concoction; //available_amount($item[none]) always = 0
				}
			}
			
			foreach SHC in Superdrink evaluate(SHC);
			if(superdrink_use_sake) foreach SSD in Supersake evaluate(SSD);
			if(superdrink_use_salty) foreach SSC in Supersalty evaluate(SSC);
			if(superdrink_use_oozeo) evaluate($item[bottle of Ooze-o]);
			vprint("winelist = "+winelist,"#FFA500",10);
			if(winelist==$item[none]){ //What if none can be made?
				if(superdrink_preference=="salty:any") superdrink_preference="stat:single";
				else if(superdrink_preference=="any:none" || vars["superdrink_only_preferred"].to_boolean()){
					vprint("Can't make anything right now, sorry!","red",2); //Restrictive...
					break; //break while loop
				}
				else switch(excise(superdrink_preference,"",":")){ //...compared to looser settings
				case "garnish":
					if(excise(superdrink_preference,":","")=="most") superdrink_preference="any:none";
					else superdrink_preference="garnish:most";
					break;
				case "stat":
					if(excise(superdrink_preference,":","")=="single") superdrink_preference="any:none";
					else superdrink_preference="stat:single";
					break;
				case "refine": 
				case "sake":
				case "drink": superdrink_preference="stat:single"; break;
				}
			}
			vprint("Attempting to make one "+winelist,"blue",4); //make us a drink!
			if(superdrink_canImallbuy){ //Can I buy ingredients?
				if(!superdrink_judge_value) time_to_profit(winelist);
				vprint("buildprice["+winelist+"] = "+superdrink_buildprice[winelist],"#FFA500",10);
				if(superdrink_buildprice[winelist]>my_meat()){
					vprint("You don't have enough meat to use this properly! Making drinks without the mall...","red",2);
					vprint("setting superdrink_canImallbuy=false","#FFA500",10);
					superdrink_canImallbuy=false;
					continue; //Not for too poor people!
				}
				else time_to_buy(winelist);
			} //If we can and need to buy ingredients from the mall, go ahead
			create(1,winelist); drinks_made[winelist]+=1;
		}
	}
	finally{
		batch_open();
		foreach boozer,amount in drinks_made{
			string p_c(int amount,item bean){
				return amount==1? "1 "+bean:amount+" "+to_plural(bean);
			}
			if(superdrink_clan_stash){ //If we're putting drinks in the stash
				vprint("Putting "+p_c(amount,boozer)+" in clan stash","purple",6);
				put_stash(amount,boozer);
			} else if(superdrink_sell_all_day){ //As long as you know what you're doing
				vprint("Putting "+p_c(amount,boozer)+" in mall for "+floor(cost_of_business(boozer)*95/100)+" meat","purple",6);
				put_shop((floor(cost_of_business(boozer)*95/100)),0,amount,boozer);
			} else vprint("Added "+p_c(amount,boozer)+" to your collection","purple",6);
		}
		if(batch_close()) vprint("Finished making the good stuff!","green",1); //The deed is done.
		else vprint("Something went wrong with transferring drinks. Check your logs.","red",1);
	}
	return;
}
//Finish heavy work!
//Functions to run with
void make_superdrinks(){ //Call this function if you want to import the script.
	if(!prereq_supermixer()){ last_call(); return; }
	else { supermixer(); last_call(); }
}
	
void main(){ //Otherwise, just run the script
	if(!prereq_supermixer()){
		last_call();
		vprint("You can't use the superdrinks script now.","red",1);
		exit;
	} else {
		try { supermixer(); }
		finally {
			last_call();
			vprint("All done making superdrinks for now!","green",1);
			exit;
		}
	}
}